package dy.spec.edu.week14_home01;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText editext1;
    private EditText editext2;
    private String stuNumber;
    private String passWord;
    private int n = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Button button = (Button) findViewById(R.id.btn);
        editext1 = (EditText) findViewById(R.id.edit);
        editext2 = (EditText) findViewById(R.id.edit1);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                stuNumber = editext1.getText().toString();
                passWord = editext2.getText().toString();
                sendkhttp();
                break;
            default:
                break;
        }

    }

    private void sendkhttp() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 创建Httpclient对象
                HttpClient httpClient = new DefaultHttpClient();

                // 创建http POST请求
                HttpPost httpPost = new HttpPost("http://www.yyjsx.xyz/api/student/login");

                // 设置2个post参数，一个是scope、一个是q
                List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
                parameters.add(new BasicNameValuePair("studentNumber", stuNumber));
                parameters.add(new BasicNameValuePair("password", passWord));
                // 构造一个form表单式的实体
                UrlEncodedFormEntity formEntity = null;
                try {
                    formEntity = new UrlEncodedFormEntity(parameters);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                // 将请求实体设置到httpPost对象中
                httpPost.setEntity(formEntity);


                try {
                    // 执行请求
                    HttpResponse response = httpClient.execute(httpPost);
                    // 判断返回状态是否为200
                    if (response.getStatusLine().getStatusCode() == 200) {
                        String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                        System.out.println(content);
                        n=content.indexOf("true");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(n!=-1){
                                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                    intent.putExtra ( "text", editext2.getText().toString() );
                                    startActivity(intent);
                                }else {
                                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }



}
